#!/bin/python3
# -*- coding:utf-8 -*-
"""
    [模块名]
    By :陈狍子 e4ting@qq.com 2024-12-14 20:41:28
"""
import sys,os
from pdb import set_trace as strace
from traceback  import format_exc as dumpstack

from swampyer import WAMPClient,WAMPClientTicket
from e4ting import util,log

from config import WampSetting

class Control():
    def __init__(self, nodeid=None):
        self.nodeid = nodeid   #    nodeid.lower()

    def callback(self, func_name="", *args, **kwargs):
        uri = ".".join(["e4ting", str(self.nodeid), func_name])
        log.info(uri)
        log.info(args)
        return self.call(uri, *args, **kwargs)

    # def publish(self, name, *data):
    #     log.info(f'[{name}] ==> {data}')
    #     ret = self.publish(name, {'acknowledge':False}, args=data)
    #     return ret

    # def subscribe(self, name):
    #     ret = self.subscribe(f'{self.preuri}.{name}')
    #     return ret

    # def register(self, name, func):
    #     uri = ".".join(["e4ting", str(self.nodeid), name])
    #     log.info(("register : ", uri))
    #     ret = self.register(uri, func)
    #     return ret

    # def nodes(self):
    #     return self.not_me(self.call('wamp.session.list'))

    def get(self, _id):
        res = self.call('wamp.registration.get', _id)
        return res

    def list(self):
        # strace()
        res = self.call('wamp.registration.list')
        return res

    def list_callees(self, _id):
        return self.call('wamp.registration.list_callees', _id)

    def lookup(self, _id):
        return self.call('wamp.registration.match', str(_id))

    def list_rpc(self):
        # strace()
        return [ self.get(_) for _ in self.list()["exact"] ]

    def list_uri(self):
        return [ _["uri"] for _ in self.list_rpc() ]

    def list_fuse(self):
        return [ _.split('.')[1] for _ in self.list_uri() if _.endswith("fuse")]

class WAMP(WAMPClientTicket, metaclass=util.Single):
    def __init__(self, uid=""):
        log.info(WampSetting().wampserver)
        super(WAMP, self).__init__(url=WampSetting().wampserver,
                                 realm=WampSetting().realm,
                              username=WampSetting().authid,
                              password=WampSetting().ticket,
                               timeout=WampSetting().timeout)
        self.start()

    # def __del__(self):
    #     log.debug("wamp 退出 {self.url}, {self.realm}".format(self=self))
    #     if not self.is_disconnected():
    #         self.disconnect()
    #     self.shutdown()


# class WAMP(WAMPClientTicket, metaclass=util.Single):
#     def __init__(self, uid=""):
#         super(WAMPClient, self).__init__(url=WampSetting().wampserver,
#                                        realm=WampSetting().realm,
#                                     username=WampSetting().authid,
#                                     password=WampSetting().ticket,
#                                      timeout=WampSetting().timeout)
#         self.start()

#     def __del__(self):
#         log.debug("wamp 退出 {self.url}, {self.realm}".format(self=self))
#         if not self.is_disconnected():
#             self.disconnect()
#         self.shutdown()
